package com.Web.Class;

import com.DAO.Impl.ClassDAOImpl;
import com.DAO.Impl.ScheduleDAOImpl;
import com.entity.Class;
import com.google.gson.Gson;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;

/**
 * @author 20x
 * @date 2024 - 11 -29
 **/
@WebServlet("/UpdataClass")
public class UpdataClassServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=application/json");
        PrintWriter out = resp.getWriter();
        Gson gson = new Gson();
        String classType = req.getParameter("classType");
        String weekOfMonth = req.getParameter("weekOfMonth");
        String CoachName = req.getParameter("CoachName");
        String ClassName = req.getParameter("ClassName");
        String site = req.getParameter("site");
        String id = req.getParameter("id");
        String time = req.getParameter("time");
        String StudentId = req.getParameter("StudentId");
        String[] StudentIdArray = StudentId.split(",");
        List<String> StudentIdList = Arrays.asList(StudentIdArray);
        System.out.println(StudentIdList);
        int count = new ClassDAOImpl().updataClass(id,time,ClassName,site,CoachName);
        int dropStu =  new ScheduleDAOImpl().dropCourse(id,time);
        System.out.println("删除学生"+dropStu);
        int AddStu = 0;
        for (String StudentId1 : StudentIdList){
            new ScheduleDAOImpl().addCourseed(id,StudentId1,time,"已到");
        }
        if(count > 0){
            List<Class> classList = new ClassDAOImpl().select(classType,weekOfMonth);
            String json = gson.toJson(classList);
            out.write(json);
        }else{
            out.write(gson.toJson("{\"message\":\"修改失败\"}"));
        }
    }
}
